package com.erp.erp_ui.Cost.Controller;

import com.alibaba.fastjson.JSONObject;
import com.erp.erp_entitys.Activiti.LeaveTaskEntity;
import com.erp.erp_entitys.Cost.entity.CostEntity;
import com.erp.erp_entitys.Cost.req.*;
import com.erp.erp_entitys.Cost.vo.*;
import com.erp.erp_entitys.Role.RoleEntity;
import com.erp.erp_entitys.Role.RoleExample;
import com.erp.erp_entitys.RoleGroupMember.RoleGroupMemberEntity;
import com.erp.erp_entitys.User.UserEntity;
import com.erp.erp_entitys.User.UserEntityExample;
import com.erp.erp_servers.Cost.CostService;
import com.erp.erp_servers.Role.IRoleService;
import com.erp.erp_servers.RoleGroupMember.IRoleGroupMemberService;
import com.erp.erp_servers.User.IUserService;
import com.erp.erp_ui.Activiti.ProcessService;
import com.erp.erp_ui.Activiti.service.IProcessSerivce;
import com.erp.erp_ui.Cost.Req.PayeeReq;
import com.erp.erp_ui.Cost.Service.CostAllService;
import com.erp.erp_ui.Cost.VO.MergeCostDetailVO;
import com.erp.erp_ui.Cost.VO.MergeDeptAccVO;
import com.erp.erp_ui.Cost.VO.MergeSelectPrimaryVO;
import com.erp.erp_ui.base.Base;
import com.erp.utils.R;
import com.erp.utils.ResultStatusEnum;
import com.github.pagehelper.PageInfo;
import org.activiti.engine.delegate.Expression;
import org.activiti.engine.impl.pvm.process.ActivityImpl;
import org.activiti.engine.impl.task.TaskDefinition;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;


/**
 * @author zjk
 * @version 1.0
 * @description 费用控制层
 * @date 2020/11/17/017 23:25
 */
@RestController
@RequestMapping(value = "cost")
public class CostController extends Base {

    /**
     * 日志
     */
    private static final Logger log= LoggerFactory.getLogger(CostController.class);

    /**
     * 费用接口
     */
    private final CostService costService;
    private final CostAllService costAllService;
    private final IProcessSerivce processSerivce;
    private final IUserService userService;
    private final IRoleService roleService;
    private final IRoleGroupMemberService roleGroupMemberService;
    @Autowired
    public CostController(CostService costService, CostAllService costAllService, IProcessSerivce processSerivce, IUserService userService, IRoleService roleService, IRoleGroupMemberService roleGroupMemberService) {
        this.costService = costService;
        this.costAllService = costAllService;
        this.processSerivce = processSerivce;
        this.userService = userService;
        this.roleService = roleService;
        this.roleGroupMemberService = roleGroupMemberService;
    }
    /**
     * 获取登陆用户信息
     * @return 登陆用户信息
     */
    @GetMapping("/getLoginUserInfo")
    @ResponseBody
    public R getLoginUserInfo(){
        log.info("仓库管理-其它出库-获取登陆用户信息");
        try {
            UserEntity user = (UserEntity)this.getBean("user");
            return R.build(user);
        }catch (Exception e){
            log.info("仓库管理-其它出库-获取登陆用户信息异常:" + e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }

    /**
     * 费用主页按条件分页展示数据
     * @param req    以实体作为参数查询
     * @return  费用集合分页数据
     */
    @GetMapping(value = "listByPage")
    public R listByPage(QueryCostReq req){
        log.info("费用主页按条件分页展示数据，条件为："+req.toString());
        try {
            PageInfo<CostEntity> pageInfo= costService.findAllCost(req);
            return R.build(0,"",pageInfo.getTotal(),pageInfo.getList());
        } catch (Exception e) {
            log.error("费用分页功能异常",e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }

    }
    /**
     * 费用主页按条件分页展示数据
     * @param req    以实体作为参数查询
     * @return  费用集合分页数据
     */
    @GetMapping(value = "getCostResListByPage")
    public R getCostResListByPage(QueryCostReq req){
        log.info("费用主页按条件分页展示数据，条件为："+req.toString());
        try {
            PageInfo<QueryCostRes> pageInfo= costService.findAllQueryCostRes(req);
            return R.build(0,"",pageInfo.getTotal(),pageInfo.getList());
        } catch (Exception e) {
            log.error("费用分页功能异常",e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }

    }
    /**
     * 获取所有制单公司
     * @return
     */
    @GetMapping(value = "getAllBillCompany")
    public R getAllBillCompany(){
        log.info("获取所有制单公司开始");
        try {
            List<QueryBillCompanyVO>  billCompanyVOList=costService.selectCompanyForBillCompanyCode();
            return R.build(billCompanyVOList);
        } catch (Exception e) {
            log.error("获取所有制单公司功能异常",e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }

    }
    /*
     * 查询是否财务人员
     * */
    @GetMapping("/isFinance")
    @ResponseBody
    public R<String> isFinance(){
        UserEntity user = (UserEntity) this.getBean("user");
        String roles=user.getRoles();
        //是否财务
        if (roles.contains("CW01")){
            return R.build("是");
        }else {
            return R.build("否");
        }
    }

    //根据费用编号获取费用详情
    @PostMapping("getCostDetailByCostCode")
    @ResponseBody
    public R getCostDetailByCostCode(@RequestBody String costCode){
        try {
            log.info("费用->查看费用单据，costCode="+costCode);

            MergeCostDetailVO mergeCostDetailVO=costAllService.getCostAll(costCode);
            return R.build(mergeCostDetailVO);

        } catch (Exception e) {
            log.error("费用->查看费用单据功能异常",e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }




    //添加开始
    //------------------------------------------------------------------------------------------------------
    //获取所有收款单位
    @GetMapping("getAllPayee")
    public R getAllPayee(){
        try {
            log.info("准备获取下拉框所需要的数据");
            List<QueryPayeeVO> payeeVOS=costAllService.AllPayee();
            return R.build(payeeVOS);

        } catch (Exception e) {
            log.error("费用->获取下拉框所需要的数据功能异常",e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }
    //获取所有收款单位
    @GetMapping("getPayeeBankInfo")
    public R getPayeeBankInfo(){
        try {
            log.info("准备获取所有收款单位");
            List<QueryPayeeVO2> payeeVOS=costService.selectPayeeBankInfo();
            return R.build(payeeVOS);

        } catch (Exception e) {
            log.error("费用->获取所有收款单位功能异常",e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }


    //获取下拉框数据
    @PostMapping("getSelectPrimary")
    public R getSelectPrimary(){
        try {
            log.info("准备获取下拉框所需要的数据");
            MergeSelectPrimaryVO selectPrimaryVO=costAllService.getSelectPrimary();
            return R.build(selectPrimaryVO);

        } catch (Exception e) {
            log.error("费用->获取下拉框所需要的数据功能异常",e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }
    /**
     * 根据收款单位编号获取银行信息
     * @param payeeCode
     * @return
     */
    @PostMapping("getPayeeBankName")
    public R getPayeeBankName(@RequestBody String payeeCode){
        try {
            log.info("准备获取银行名称");
            List<QueryPayeeBankVO> payeeBankVOS=costService.selectPayeeBankName(payeeCode);
            return R.build(payeeBankVOS);

        } catch (Exception e) {
            log.error("费用->获取银行名称功能异常",e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }

    //获取银行账号
    @PostMapping("getPayeeBankAccount")
    public R getPayeeBankAccount(@RequestBody PayeeReq payeeReq){
        try {
            log.info("准备获取银行名称");
            List<QueryPayeeBankVO> payeeBankVOS=costService.selectPayeeBankAccount(payeeReq.getBankName(),payeeReq.getPayeeCode());
            return R.build(payeeBankVOS);

        } catch (Exception e) {
            log.error("费用->获取银行名称功能异常",e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }

    /**
     * 获取银行账号名称
     * @param payeeCode 供应商编码
     * @param bankAccount 银行账号
     * @return
     */
    @GetMapping("getPayeeBankAccountName")
    public R getPayeeBankAccountName(String payeeCode,String bankAccount){
        try {
            log.info("获取银行账号名称");
            String accountName=costService.selectPayeeBankAccountName(payeeCode,bankAccount);
            return R.build(accountName);

        } catch (Exception e) {
            log.error("费用->获取银行账号名称功能异常",e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }


    //获得编号
    @GetMapping("geCostCode")
    @ResponseBody
    public R geCostCode(){
        try {
            log.info("准备效验编号");
            String code=costService.selectCodeByCode();
            return R.build(code);

        } catch (Exception e) {
            log.error("费用->效验编号功能异常",e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }


    //获得币种,,暂时不管


    //获得制单公司
    @GetMapping("geBillCompany")
    @ResponseBody
    public R geBillCompany(){
        try {
            log.info("准备获取制单公司");
            List<QueryBillCompanyVO>  billCompanyVOList=costService.selectCompanyForBillCompanyCode();
            return R.build(billCompanyVOList);

        } catch (Exception e) {
            log.error("费用->获取制单公司功能异常",e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }

    /**
     * 根据制单公司编号获得制单公司详情
     * @return
     */
    @PostMapping("geBillCompany")
    @ResponseBody
    public R geBillCompanyByCode(@RequestBody String billCompanyCode){
        try {
            log.info("准备获取制单公司");
            QueryBillCompanyVO billCompanyVO=costService.selectCompanyForBillCompanyCode2(billCompanyCode);
            return R.build(billCompanyVO);

        } catch (Exception e) {
            log.error("费用->获取制单公司功能异常",e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }


    //获取收款单位信息
    //根据收款单位的编号查询旗下所对应的单位名称，银行账号，银行名称，如果制单单位编号为空则查询所有制单单位
    @GetMapping("geCostPayee")
    @ResponseBody
    public R geCostPayee(String costPayeeCode){
        try {
            log.info("准备获取收款单位");
            List<QueryPayeeVO>  payeeVOS=costService.selectCustomer2ForPayee();
            return R.build(payeeVOS);

        } catch (Exception e) {
            log.error("费用->获取收款单位功能异常",e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }
    //获取部门,核算员工
    @PostMapping("getCostDetailDeptAcc")
    @ResponseBody
    public R getCostDetailDept(QueryDeptAccReq deptAccReq){
        try {
            log.info("准备获取部门,核算员工");
            MergeDeptAccVO deptAccVO=costAllService.getDeptAcc(deptAccReq);
            return R.build(deptAccVO);

        } catch (Exception e) {
            log.error("费用->获取部门,核算员工.功能异常",e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }

    //***********************************************************************************

    //获取部门
    @GetMapping("getDept")
    @ResponseBody
    public R getDept(){
        try {
            log.info("准备获取部门,核算员工");
            List<QueryDeptVO2> deptVO=costService.selectDept();
            return R.build(deptVO);
        } catch (Exception e) {
            log.error("费用->获取部门,核算员工.功能异常",e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }
    //获取人员
    @GetMapping("getAcc")
    @ResponseBody
    public R getAcc(){
        try {
            log.info("准备获取部门,核算员工");
            List<QueryAccVO2>  accVO=costService.selectAcc();
            return R.build(accVO);
        } catch (Exception e) {
            log.error("费用->获取部门,核算员工.功能异常",e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }

    //根据部门编号获取核算人员集合
    @RequestMapping("getAccByDeptCode")
    @ResponseBody
    public R getAccByDeptCode(String deptCode){
        try {
            log.info("根据部门编号获取核算人员集合");
            List<QueryAccVO2>  accVO=costService.getAccByDeptCode(deptCode);
            return R.build(accVO);
        } catch (Exception e) {
            log.error("费用->根据部门编号获取核算人员集合功能异常",e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }


    //获取直接费用名称,
    // 直接费用ty5，lx19。。间接费用ty5，lx43
    @PostMapping("getCostName")
    @ResponseBody
    public R getCostName(@RequestBody DictionaryCostReq req){
        try {
            log.info("获取费用名称");
            List<DictionaryCostFeeVO>  costNames=costService.getDictionaryCosFee(req);
            return R.build(costNames);
        } catch (Exception e) {
            log.error("费用->获取费用名称功能异常",e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }




//***********************************************************************************

    //导入资源样品获取资源样品详情
    @GetMapping("getResourceSamplesListByPage")
    public R getResourceSamplesByPrimary(QueryImportResourceSamplesReq importResourceSamplesReq){
        try {
            log.info("导入资源样品获取资源样品详情");
            PageInfo<QueryImportResourceSamplesVO> pageInfo=costService.selectResourceSamplesByPrimary(importResourceSamplesReq);
            return R.build(0,"",pageInfo.getTotal(),pageInfo.getList());
        } catch (Exception e) {
            log.error("费用->导入资源样品获取资源样品详情功能异常",e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }


    //导入发票获取发票详情
    @GetMapping("getInvoiceListByPage")
    public R getInvoiceByPrimary(QueryImportInvoiceReq importInvoiceReq){
        try {
            log.info("导入发票获取发票详情");
            PageInfo<QueryImportInvoiceVO> pageInfo=null;
            if (StringUtils.isNotEmpty(importInvoiceReq.getInvoiceCode())){
                importInvoiceReq.setInvoiceCode(importInvoiceReq.getInvoiceCode().trim());
            }
            if (StringUtils.isNotEmpty(importInvoiceReq.getContractNo())){
                importInvoiceReq.setContractNo(importInvoiceReq.getContractNo().trim());
            }

            if(importInvoiceReq.getInvoiceType()==""||importInvoiceReq.getInvoiceType()==null||importInvoiceReq.getInvoiceType()=="1"||importInvoiceReq.getInvoiceType().equals("1")){
                pageInfo = costService.selectInvoiceByInvoiceTypeOne(importInvoiceReq);
            }else{
                pageInfo = costService.selectInvoiceByInvoiceTypeTwo(importInvoiceReq);
            }
            return R.build(0,"",pageInfo.getTotal(),pageInfo.getList());
        } catch (Exception e) {
            log.error("费用->导入发票获取发票详情功能异常",e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }


    //导入合同获取合同详情
    @GetMapping("getContractListByPage")
    public R getContractByPrimary(QueryImportContractReq importContractReq){
        try {
            log.info("准备合同详情");
            PageInfo<QueryImportContractVO>  pageInfo=null;
            if(importContractReq.getContractType()==""||importContractReq.getContractType()==null||importContractReq.getContractType()=="1"||importContractReq.getContractType().equals("1")){
                pageInfo=costService.selectExportContractByPrimary(importContractReq);
            }else {
                pageInfo=costService.selectPurchaseContractByPrimary(importContractReq);
            }
            return R.build(0,"",pageInfo.getTotal(),pageInfo.getList());

        } catch (Exception e) {
            log.error("费用->导入合同获取合同详情功能异常",e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }


    //获取所有用户
    @GetMapping("getAllUser")
    @ResponseBody
    public R getAllUser(){
        try {
            log.info("获取所有用户");
            List<QueryAllUserVO>  userVOS=costService.selectAllUser();
            return R.build(userVOS);
        } catch (Exception e) {
            log.error("费用->获取所有用户功能异常",e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }

    //修改，添加费用
    @PostMapping("add/register")
    @ResponseBody
    public R registerCost(@RequestBody ModifyAndInsertCostReq2 datas){
        try {
            log.info("修改，添加费用");
            R r=costAllService.ModifyAndInsertCost2(datas);
            return r;
        } catch (Exception e) {
            log.error("费用->修改，添加费用功能异常",e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }

    /**
     * 删除费用主信息,以及费用明细
     * @param costId
     * @return
     */
    @PostMapping("delCostByCostId")
    @ResponseBody
    public R delCostByCostId(@RequestBody Integer costId){
        try {
            log.info("删除操作");
            return costAllService.delCostByCostId(costId);
        } catch (Exception e) {
            log.error("费用->修改费用以及关联的数据为无效功能异常",e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }
    /**
     * 修改费用状态
     * @param req   入参
     * @return
     */
    @PostMapping("updateCostTypeByCostId")
    @ResponseBody
    public R updateCostTypeByCostId(@RequestBody UpdateCostStatusReq req){
        try {
            log.info("修改费用状态");
            return costAllService.updateCostTypeByCostId(req.getCostIds(),req.getStatus());
        } catch (Exception e) {
            log.error("费用->修改费用状态功能异常",e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }
    /**
     * 还原费用状态，从垃圾箱还原到草稿箱
     * @param
     * @return
     */
    @PostMapping("updateCostStatusDustbinToDrafts")
    @ResponseBody
    public R updateCostStatusDustbinToDrafts(@RequestBody Integer id){
        try {
            log.info("还原费用状态，从垃圾箱还原到草稿箱");
            return costService.updateCostStatusDustbinToDrafts(id);
        } catch (Exception e) {
            log.error("费用->还原费用状态，从垃圾箱还原到草稿箱功能异常",e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }


    //拆分开始--------------------------------------------------------
    /**
     * 拆分费用
     * @param
     * @return
     */
    @PostMapping("SplitCost")
    @ResponseBody
    public R SplitCost(@RequestBody Integer id){
        try {
            log.info("还原费用状态，从垃圾箱还原到草稿箱");
            return costService.updateCostStatusDustbinToDrafts(id);
        } catch (Exception e) {
            log.error("费用->还原费用状态，从垃圾箱还原到草稿箱功能异常",e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }


    /**
     * 根据id查找费用实体
     * @param
     * @return
     */
    @PostMapping("findCostById")
    @ResponseBody
    public R findCostById(@RequestBody Integer id){
        try {
            log.info("根据id查找费用实体");
            CostEntity costEntity = costService.findCostById(id);
            return R.build(costEntity);
        } catch (Exception e) {
            log.error("费用->根据id查找费用实体功能异常",e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }




    //**********************************费用申请，工作流开始********************************************************
    /**
     * 更新费用审批的状态
     * @param req   入参
     * @return
     */
    @PostMapping("updateCostStatusByCostId")
    @ResponseBody
    public R updateCostStatusByCostId(@RequestBody UpdateCostStatusBpmReq req){
        try {
            log.info("更新费用状态");
            return costService.updateCostStatusByCostId(req);
        } catch (Exception e) {
            log.error("费用->更新费用状态功能异常",e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }
    /**
     * 根据流程实例id获取任务id
     * @param instanceId
     * @return
     */
    @PostMapping("getTasKId")
    @ResponseBody
    public R getTasKId(@RequestBody String instanceId){
        try {
            log.info("根据流程实例id获取任务id");
            String taskId="";
            UserEntity user = (UserEntity)this.getBean("user");
            String assignee=user.getCode();
            List<LeaveTaskEntity> leaveTaskEntityList= ProcessService.queryToDoTasks2(assignee,"cost");
            for (LeaveTaskEntity leaveTaskEntity:leaveTaskEntityList) {
                String processInstanceId=leaveTaskEntity.getProcessInstanceId();
                if (instanceId==processInstanceId||instanceId.equals(processInstanceId)){
                    taskId=leaveTaskEntity.getTaskid();
                }
            }
            return R.build(taskId);
        } catch (Exception e) {
            log.error("费用->根据流程实例id获取任务id功能异常",e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }


    /**
     * 提交流程/流程转向
     * @param taskId
     *            当前任务ID
     * @param variables
     *            流程变量
     * @param activityId
     *            流程转向执行任务节点ID<br>
     *            此参数为空，默认为提交操作
     * @throws Exception
     */
    @RequestMapping(value = "commitProcess")
    @ResponseBody
    public R<Boolean> commitProcess(String taskId, Map<String, Object> variables, String activityId, String comment){
        try{
            ActivityImpl activitiImpl = ProcessService.findActivitiImpl(taskId, activityId);
            comment=activitiImpl.getProperty("name")+"通过["+comment+"]";
            //判断当前节点是否是最后一个节点

            Map<String,String> map=ProcessService.isFinalTask(taskId,activityId);

            UserEntity user = (UserEntity)this.getBean("user");
            //TaskDefinition nextTask = ProcessService.getNextTask(taskId);
            variables.putAll( getNextSpr( taskId));//获取下一节点审批人
            // getNextSpr( taskId);
            ProcessService.commitProcess(taskId,variables,activityId,comment,user.getName());
            if(map.get("tf").equals("true")){

                boolean b = processSerivce.updateStatus(map.get("processKey"), map.get("bid"),"1");
            }
            return R.build(true);
        }catch (Exception e){
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }

    }
    /**
     * 获取下一节点审批人
     * @param taskId
     * @return
     * @throws Exception
     */
    private Map<String, Object> getNextSpr(String taskId)throws Exception{
        Map<String, Object> map=new HashMap<>();
        TaskDefinition nextTask = ProcessService.getNextTask(taskId);
        if(nextTask==null) return map;
        Expression assigneeExpression = nextTask.getAssigneeExpression();
        Set<Expression> candidateUserIdExpressions = nextTask.getCandidateUserIdExpressions();
        Set<Expression> candidateGroupIdExpressions = nextTask.getCandidateGroupIdExpressions();
        String a="";
        String u="";
        String g="";
        String ru="";
        String rg="";
        if(candidateUserIdExpressions.size() >0){
           /* String role=cl(candidateUserIdExpressions.iterator().next());
            variables.put(role,getUserByRole(role));*/
            ru = candidateUserIdExpressions.iterator().next().getExpressionText().replace("${","").replace("}","");
            u=getUserByRole(ru);
        }
        if(candidateGroupIdExpressions.size()>0){
            rg= candidateGroupIdExpressions.iterator().next().getExpressionText().replace("${","").replace("}","");
            g=getUserGroupByRole(rg);
        }
        if(assigneeExpression !=null){
            a=assigneeExpression.getExpressionText();
        }

        /*if(a !=null)   return a;*/
        if(!ru.equals("")){
            map.put(ru,u);
        }
        if(!rg.equals("")) {
            map.put(rg,g);
        }
        if(!a.equals("")  && a.indexOf("$") !=-1){
            map.put(a.replace("${","").replace("}",""),a);
        }

        return map;
    }
    private String getUserByRole(String role){
        UserEntityExample example=new UserEntityExample();
        UserEntityExample.Criteria criteria = example.createCriteria();
        criteria.andRolesLike("%"+role+"%");
        List<UserEntity> userEntities = userService.selectByExample(example);
        //  List<String> list=new ArrayList<>();
        String user="";
        for(int i=0;i<userEntities.size();i++){
            if(i==userEntities.size()-1){
                user+=userEntities.get(i).getCode();
            }else{
                user+=userEntities.get(i).getCode()+",";
            }

        }
        return user;
    }
    private String getUserGroupByRole(String grouprole){
        try{
            String users="";
            List<RoleEntity> roleEntities=new ArrayList<RoleEntity>();

            RoleGroupMemberEntity record=new RoleGroupMemberEntity();
            record.setGroupCode(grouprole);
            List<RoleGroupMemberEntity> roleGroupMemberEntities = roleGroupMemberService.selectByExample(record);
            if(roleGroupMemberEntities.size()==0) return users;
            List<String> listRoleCode=new ArrayList<String>();
            for(int i=0;i<roleGroupMemberEntities.size();i++){
                listRoleCode.add(roleGroupMemberEntities.get(i).getRoleCode());
            }
            RoleExample example=new RoleExample();
            RoleExample.Criteria criteria = example.createCriteria();
            criteria.andCodeIn(listRoleCode);
            roleEntities = roleService.selectByExample(example);
            //获取人
            UserEntityExample example1=new UserEntityExample();
            UserEntityExample.Criteria criteria1 = example1.createCriteria();
            for(int i=0;i<roleEntities.size();i++){
                criteria1.andRolesLike("%"+roleEntities.get(i).getCode()+"%");
            }
            List<UserEntity> userEntities = userService.selectByExample(example1);
            for(int i=0;i<userEntities.size();i++){
                if(i==userEntities.size()-1){
                    users+=userEntities.get(i).getCode();
                }else{
                    users+=userEntities.get(i).getCode()+",";
                }
            }
            return users;
        }catch (Exception e){
            e.printStackTrace();
            return "";
        }

    }








    //**********************************费用统计开始************************************


    /**
     * 费用统计-分页
     * @param req
     * @return
     */
    @RequestMapping("costStatistics1ListByPage")
    @ResponseBody
    public R costStatistics1ListByPage(QueryCostStatistics1Req req){
        try {
            log.info("费用统计");
            PageInfo<QueryCostStatistics1VO> pageInfo=costAllService.costStatistics1ListByPage(req);
            return R.build(0,"",pageInfo.getTotal(),pageInfo.getList());
        } catch (Exception e) {
            log.error("费用->费用统计功能异常",e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }

    /**
     * 费用统计-导出
     */
    @RequestMapping("costStatistics1Export")
    @ResponseBody
    public R costStatistics1Export(QueryCostStatistics1Req req){
        try {
            log.info("费用统计-导出req="+req.toString());
            JSONObject info= costAllService.costStatistics1Export(req);
            return R.build(info);
        } catch (Exception e) {
            log.error("费用->费用统计-导出异常req="+req.toString(),e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }

    /**
     * 费用支付统计-分页
     * @param req
     * @return
     */
    @RequestMapping("costStatistics2ListByPage")
    @ResponseBody
    public R costStatistics2ListByPage(QueryCostStatistics1Req req){
        try {
            log.info("费用支付统计");
            PageInfo<QueryCostStatistics2VO> pageInfo=costAllService.costStatistics2List(req);
            return R.build(0,"",pageInfo.getTotal(),pageInfo.getList());
        } catch (Exception e) {
            log.error("费用->费用支付统计功能异常",e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }

    /**
     * 费用支付统计-导出
     */
    @RequestMapping("costStatistics2Export")
    @ResponseBody
    public R costStatistics2Export(QueryCostStatistics1Req req){
        try {
            log.info("费用支付统计-导出req="+req.toString());
            JSONObject info= costAllService.costStatistics2Export(req);
            return R.build(info);
        } catch (Exception e) {
            log.error("费用->费用支付统计-导出异常req="+req.toString(),e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }


    /**
     * 根据id集合查询费用，不分页
     * @param ids
     * @return
     */
    @RequestMapping("findCostByIds")
    @ResponseBody
    public R findCostByIds(String ids){
        try {
            log.info("根据id集合查询费用 ids="+ids);
            List<QueryCostRes> list=costService.findCostByIds(ids);
            return R.build(list);
        } catch (Exception e) {
            log.error("费用->根据id集合查询费用功能异常",e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }

}
